<?php

require_once ('ConnectDB.php');
require_once ('GianHangDTO.php');

/** 
 * @author Thu Ha
 * 
 * 
 */
class GianHangDAO extends ConnectDB {
	//TODO - Insert your code here
	function GianHangDAO()
	{
		
	}
	/**
	 * Thêm gian hàng
	 */
	public static function ThemGianHang($gianHangDto)
	{
		$result = true;
		try 
		{
			if (!ConnectDB::OpenConnection())
				return FALSE;
						
			$strSql = "	
					insert into gian_hang(
					`MaGianHang`, `MaNguoiDung`, `TenGianHang`, `NgayTao`, `NgayXoa`, `NguoiXoa`,
					`NgayCapNhat`, `NguoiCapNhat`, `Theme`, `DaXoa`, `LuotXem`,`ThongTin`)	
					values('$gianHangDto->MaGianHang',
			 				'$gianHangDto->MaNguoiDung',
							'$gianHangDto->TenGianHang',
							'$gianHangDto->NgayTao',
							'',
							'',
							'',
							'',
							'$gianHangDto->Theme',
							$gianHangDto->DaXoa,
							$gianHangDto->LuotXem,
							'$gianHangDto->ThongTin');";
							
			//echo $strSql;
			$result = mysql_query($strSql, ConnectDB::$mLink);
				
			ConnectDB::CloseConnection();			
		} 
		catch (Exception $e) {
			$result = FALSE;
		}
		return $result;
	}
	
	/**
	 * Tìm kiếm gian hàng theo Tên gian hàng, ngày tạo
	 * edit by Thu Hà 25/6/2011
	 */
	public static function TimKiemGianHang($TenGianHang, $NgayTao_Tu, $NgayTao_Den)
	{
		$lstGianHang = array();
		try {
			if (!ConnectDB::OpenConnection())
			return FALSE;
			
			$strSQL = "SELECT * FROM gian_hang WHERE DaXoa = 0 AND TenGianHang Like'%$TenGianHang%'";
			if (strlen($NgayTao_Tu) > 0)
			{
				$strSQL .= " AND NgayTao >= '$NgayTao_Tu'";
			}
			
			if (strlen($NgayTao_Den) > 0)
			{
				$strSQL .= " AND NgayTao <= '$NgayTao_Den'";
			}
			//echo $strSQL;
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			if ($result== false || mysql_num_rows($result) <= 0)
			{
				return null;
			}
			
			while ($row = mysql_fetch_array($result))
			{
				$gianHangDto = new GianHangDTO();
				$gianHangDto->MaGianHang = $row["MaGianHang"];				
				$gianHangDto->MaNguoiDung = $row["MaNguoiDung"];	
				$gianHangDto->TenGianHang = $row["TenGianHang"];	
				$gianHangDto->NgayTao = $row["NgayTao"];	
				$gianHangDto->LuotXem = $row["LuotXem"];	
				$gianHangDto->DaXoa = $row["DaXoa"];	
				$gianHangDto->Theme = $row["Theme"];	
				$gianHangDto->NgayTao = $row["NgayTao"];	
				$gianHangDto->NgayCapNhat = $row["NgayCapNhat"];	
				$gianHangDto->NguoiCapNhat = $row["NguoiCapNhat"];	
				$gianHangDto->NguoiXoa = $row["NguoiXoa"];	
				$gianHangDto->NgayXoa = $row["NgayXoa"];
				
				if (file_exists($gianHangDto->ThongTin))
				{
					$lstThongTin = GianHangDAO::readXML($gianHangDto->ThongTin);
					$gianHangDto->Slogan = $lstThongTin["slogan"];
					$gianHangDto->DiaChi = $lstThongTin["diachi"];
					$gianHangDto->GioiThieu = $lstThongTin["gioithieu"];
					$gianHangDto->Footer = $lstThongTin["footer"];
					$gianHangDto->Logo = $lstThongTin["logo"];
					$gianHangDto->Banner = $lstThongTin["banner"];
					
				}
				

				array_push($lstGianHang, $gianHangDto);
			}
			
			ConnectDB::CloseConnection();
			
		} 
		catch (Exception $e) {
			$lstGianHang = null;
		}
		return $lstGianHang;
	}
		
	/**
	 * Lấy tất cả gian hàng xếp theo thứ tự Lượt xem, ngày tạo
	 * edit by Thu Hà 20/6/2011
	 * $number: > 0: số dòng phải lấy ra, = 0: lấy tất cả
	 */
	public static function LayTatCaGianHangTheoNgayTao($number)
	{
		$lstGianHang = array();
		try 
		{
			if (!ConnectDB::OpenConnection())
				return FALSE;
						
			$strSQL = "SELECT * FROM `gian_hang` ORDER BY `NgayTao` DESC, `LuotXem` DESC";
			if ($number > 0)
				$strSQL .= " LIMIT 0, $number ";
			$strSQL .= " ;";
			
			//echo $strSQL;
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			if (!$result || mysql_num_rows($result) <= 0)
			{				
				return null;
			}
			
			while ($row = mysql_fetch_array($result))
			{
				$gianHangDto = new GianHangDTO();
				$gianHangDto->MaGianHang = $row["MaGianHang"];				
				$gianHangDto->MaNguoiDung = $row["MaNguoiDung"];	
				$gianHangDto->TenGianHang = $row["TenGianHang"];
				$gianHangDto->NgayTao = $row["NgayTao"];
				$gianHangDto->NgayXoa = $row["NgayXoa"];
				$gianHangDto->NguoiXoa = $row["NguoiXoa"];		
				$gianHangDto->NgayCapNhat = $row["NgayCapNhat"];	
				$gianHangDto->NguoiCapNhat = $row["NguoiCapNhat"];	
				$gianHangDto->Theme = $row["Theme"];	
				$gianHangDto->DaXoa = $row["DaXoa"];
				$gianHangDto->LuotXem = $row["LuotXem"];	
				$gianHangDto->ThongTin = $row["ThongTin"];
				
				if (file_exists($gianHangDto->ThongTin))
				{
					$lstThongTin = GianHangDAO::readXML($gianHangDto->ThongTin);
					$gianHangDto->Slogan = $lstThongTin["slogan"];
					$gianHangDto->DiaChi = $lstThongTin["diachi"];
					$gianHangDto->GioiThieu = $lstThongTin["gioithieu"];
					$gianHangDto->Footer = $lstThongTin["footer"];
					$gianHangDto->Logo = $lstThongTin["logo"];
					$gianHangDto->Banner = $lstThongTin["banner"];
					
				}
				
				
				array_push($lstGianHang, $gianHangDto);
			}
			
			ConnectDB::CloseConnection();
			
		} 
		catch (Exception $e) 
		{
			$lstGianHang = null;
		}
		return $lstGianHang;
	}
	/**
	 * Lấy tất cả gian hàng xếp theo thứ tự Lượt xem, ngày cập nhật
	 * edit by Thu Hà 20/6/2011
	 * $number: > 0: số dòng phải lấy ra, = 0: lấy tất cả
	 * edit by Anh Vũ 25/6/2011
	 */
	public static function LayTatCaGianHangTheoNgayCapNhat($number)
	{
		$lstGianHang = array();
		try {
			if (!ConnectDB::OpenConnection())
			return FALSE;
						
			$strSQL = "SELECT *";
			//if ($number > 0)
				//$strSQL .= " TOP $number ";
			$strSQL .= " FROM `gian_hang` ORDER BY `NgayCapNhat` ASC, `LuotXem` DESC";
			if ($number > 0)
				$strSQL .= " LIMIT 0, $number ";
			$strSQL .= " ;";
			//echo $strSQL;
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			if ($result== false)
			{				
				return null;
			}
			
			while ($row = mysql_fetch_array($result))
			{
				$gianHangDto = new GianHangDTO();
				$gianHangDto->MaGianHang = $row["MaGianHang"];				
				$gianHangDto->MaNguoiDung = $row["MaNguoiDung"];	
				$gianHangDto->TenGianHang = $row["TenGianHang"];
				$gianHangDto->NgayTao = $row["NgayTao"];
				$gianHangDto->NgayXoa = $row["NgayXoa"];
				$gianHangDto->NguoiXoa = $row["NguoiXoa"];		
				$gianHangDto->NgayCapNhat = $row["NgayCapNhat"];	
				$gianHangDto->NguoiCapNhat = $row["NguoiCapNhat"];	
				$gianHangDto->Theme = $row["Theme"];	
				$gianHangDto->DaXoa = $row["DaXoa"];
				$gianHangDto->LuotXem = $row["LuotXem"];	
				$gianHangDto->ThongTin = $row["ThongTin"];
				
				if (file_exists($gianHangDto->ThongTin))
				{
					$lstThongTin = GianHangDAO::readXML($gianHangDto->ThongTin);
					$gianHangDto->Slogan = $lstThongTin["slogan"];
					$gianHangDto->DiaChi = $lstThongTin["diachi"];
					$gianHangDto->GioiThieu = $lstThongTin["gioithieu"];
					$gianHangDto->Footer = $lstThongTin["footer"];
					$gianHangDto->Logo = $lstThongTin["logo"];
					$gianHangDto->Banner = $lstThongTin["banner"];
					
				}
				
				
				array_push($lstGianHang, $gianHangDto);
			}
			
			ConnectDB::CloseConnection();
			
		} catch (Exception $e) {
			$lstGianHang = array();
		}
		return $lstGianHang;
	}
	
	/**
	 * Lấy gian hàng theo mã gian hàng
	 * $trangThai: 0 chưa xóa, 1: đã xóa, 2: tất cả 
	 * edit by Thu Hà 20/6/2011
	 */
	public static function LayGianHangTheoMa($maGianHang, $trangThai)
	{
		$gianHangDto = new GianHangDTO();
		try {
			if (!ConnectDB::OpenConnection())
			return FALSE;
			
			$strSQL = "SELECT *
					FROM `gian_hang`
					WHERE `MaGianHang` = $maGianHang ";
			if ($trangThai == 2)
			{
				$strSQL .=";";
			}
			else
			{
				$strSQL .=" AND `DaXoa` = $trangThai;";
			}
			//echo $strSQL;
			
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			if (!$result || mysql_num_rows($result) != 1)
			{
				return null;
			}
			
			$row = mysql_fetch_array($result);
			
			$gianHangDto->MaGianHang = $row["MaGianHang"];				
			$gianHangDto->MaNguoiDung = $row["MaNguoiDung"];	
			$gianHangDto->TenGianHang = $row["TenGianHang"];
			$gianHangDto->NgayTao = $row["NgayTao"];
			$gianHangDto->NgayXoa = $row["NgayXoa"];
			$gianHangDto->NguoiXoa = $row["NguoiXoa"];		
			$gianHangDto->NgayCapNhat = $row["NgayCapNhat"];	
			$gianHangDto->NguoiCapNhat = $row["NguoiCapNhat"];	
			$gianHangDto->Theme = $row["Theme"];	
			$gianHangDto->DaXoa = $row["DaXoa"];
			$gianHangDto->LuotXem = $row["LuotXem"];	
			$gianHangDto->ThongTin = $row["ThongTin"];	
			
			if (file_exists($gianHangDto->ThongTin))
			{
				$lstThongTin = GianHangDAO::readXML($gianHangDto->ThongTin);
				$gianHangDto->Slogan = $lstThongTin["slogan"];
				$gianHangDto->DiaChi = $lstThongTin["diachi"];
				$gianHangDto->GioiThieu = $lstThongTin["gioithieu"];
				$gianHangDto->Footer = $lstThongTin["footer"];
				$gianHangDto->Logo = $lstThongTin["logo"];
				$gianHangDto->Banner = $lstThongTin["banner"];
				
			}
						
			
			ConnectDB::CloseConnection();
			
		} catch (Exception $e) {
			$gianHangDto = null;
		}
		return $gianHangDto;
	}
	/**
	 * Lấy gian hàng theo tên gian hàng
	 * $trangThai: 0 chưa xóa, 1: đã xóa, 2: tất cả 
	 * edit by Thu Hà 20/6/2011
	 */
	public static function LayGianHangTheoTenGH($tenGianHang, $trangThai)
	{
		$gianHangDto = new GianHangDTO();
		try {
			if (!ConnectDB::OpenConnection())
			return FALSE;
			
			$strSQL = "SELECT *
					FROM `gian_hang`
					WHERE `TenGianHang` = '$tenGianHang' ";					
			if ($trangThai == 2)
			{
				$strSQL .=";";
			}
			else
			{
				$strSQL .=" AND `DaXoa` = $trangThai;";
			}
			//echo $strSQL;
			
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			if (!$result || mysql_num_rows($result) != 1)
			{
				return null;
			}
			
			$row = mysql_fetch_array($result);
			
			$gianHangDto->MaGianHang = $row["MaGianHang"];				
			$gianHangDto->MaNguoiDung = $row["MaNguoiDung"];	
			$gianHangDto->TenGianHang = $row["TenGianHang"];
			$gianHangDto->NgayTao = $row["NgayTao"];
			$gianHangDto->NgayXoa = $row["NgayXoa"];
			$gianHangDto->NguoiXoa = $row["NguoiXoa"];		
			$gianHangDto->NgayCapNhat = $row["NgayCapNhat"];	
			$gianHangDto->NguoiCapNhat = $row["NguoiCapNhat"];	
			$gianHangDto->Theme = $row["Theme"];	
			$gianHangDto->DaXoa = $row["DaXoa"];
			$gianHangDto->LuotXem = $row["LuotXem"];	
			$gianHangDto->ThongTin = $row["ThongTin"];	
			
			if (file_exists($gianHangDto->ThongTin))
			{
				$lstThongTin = GianHangDAO::readXML($gianHangDto->ThongTin);
				$gianHangDto->Slogan = $lstThongTin["slogan"];
				$gianHangDto->DiaChi = $lstThongTin["diachi"];
				$gianHangDto->GioiThieu = $lstThongTin["gioithieu"];
				$gianHangDto->Footer = $lstThongTin["footer"];
				$gianHangDto->Logo = $lstThongTin["logo"];
				$gianHangDto->Banner = $lstThongTin["banner"];
				
			}
						
			
			ConnectDB::CloseConnection();
			
		} catch (Exception $e) {
			$gianHangDto = null;
		}
		return $gianHangDto;
	}
	/**
	 * Cập nhật lượt xem
	 * Edit by Thu Hà 20/6/2011
	 */
	public static function TangLuotXemCuaGianHang($maGianHang, $maNguoiDung)
	{
		$result = true;
		try {
			if (!ConnectDB::OpenConnection())
			return FALSE;
			
			$strSQL ="UPDATE `gian_hang` 
					SET `LuotXem` = `LuotXem` + 1, `NguoiCapNhat` = $maNguoiDung, `NgayCapNhat` = now()
					WHERE `MaGianHang` = $maGianHang AND DaXoa = 0;";
			//echo $strSQL;
			$result = mysql_query($strSQL, ConnectDB::$mLink);			
			ConnectDB::CloseConnection();
			
		} catch (Exception $e) {
			$result = false;
		}
		return $result;
	}
	/**
	 * Cập nhật gian hàng
	 * Edit by Thu Hà 20/11/2011 - CHƯA XONG
	 */
	public static function CapNhatGianHang($thongTin, $theme, $maGH)
	{
		$result = true;
		try {
			if (!ConnectDB::OpenConnection())
			return FALSE;
			
			$strSQL = "UPDATE `gian_hang`
						SET `ThongTin` = '$thongTin', `Theme` = $theme 
						WHERE `MaGianHang` = $maGH";
			echo $strSQL;
				
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			ConnectDB::CloseConnection();
			
		} catch (Exception $e) {
			$result = FALSE;
		}
		return $result;
	}
	
	/**
	 * Xóa gian hàng
	 * Edit by Thu Hà 20/11/2011
	 */
	public static function XoaGianHang($maGianHang, $maNguoiDung)
	{
		$result = true;
		try {
			if (!ConnectDB::OpenConnection())
			return FALSE;
			
			$strSQL = "	UPDATE `gian_hang` 
						SET `DaXoa` = 1, `NguoiXoa` = $maNguoiDung, `NgayXoa` = now()
						WHERE `MaGianHang` = $maGianHang;";
				
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			ConnectDB::CloseConnection();
			
		} catch (Exception $e) {
			$result = FALSE;
		}
		return $result;
	}
	//25/6/2011
	// Thu Hà 
	public static function readXML($file)
	{
		$lstThongTin = array();
		$doc = new DOMDocument('1.0','utf-8');
		$doc->load($file);
		  
		$root = $doc->getElementsByTagName("ThongTin");
		foreach( $root as $ThongTin )
  		{
			$slogan = $ThongTin->getElementsByTagName("slogan");
			$lstThongTin["slogan"] = $slogan->item(0)->nodeValue;
			  
			$diachi = $ThongTin->getElementsByTagName("diachi");
			$lstThongTin["diachi"] = $diachi->item(0)->nodeValue;
			  
			$gioithieu = $ThongTin->getElementsByTagName("gioithieu");
			$lstThongTin["gioithieu"] = $gioithieu->item(0)->nodeValue;
			
			$footer = $ThongTin->getElementsByTagName("footer");
			$lstThongTin["footer"] = $footer->item(0)->nodeValue;
			
			$logo = $ThongTin->getElementsByTagName("logo");
			$lstThongTin["logo"] = $logo->item(0)->nodeValue;
			
			$banner = $ThongTin->getElementsByTagName("banner");
			$lstThongTin["banner"] = $banner->item(0)->nodeValue;
			
			break;
		}
		return $lstThongTin;			
	}
  
  	/*
		Lấy danh sách gian hàng theo mã người dùng like
		Anh Vũ 25/6/2011
	*/
	public static function LayDSGianHangTheoNguoiDungLike($maNguoiDung)
	{
		$lstGianHang = array();
		try {
			if (!ConnectDB::OpenConnection())
			return FALSE;
						
			$strSQL = "select *
						from Likes l, gian_hang gh 
						where l.MaNguoiDung = $maNguoiDung 
						and l.MaDoiTuong = gh.MaGianHang AND gh.DaXoa = 0";
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			if ($result== false)
			{				
				return null;
			}

			while ($row = mysql_fetch_array($result))
			{
				$gianHangDto = new GianHangDTO();
				$gianHangDto->MaGianHang = $row["MaGianHang"];		
				$gianHangDto->MaNguoiDung = $row["MaNguoiDung"];	
				$gianHangDto->TenGianHang = $row["TenGianHang"];
				$gianHangDto->NgayTao = $row["NgayTao"];
				$gianHangDto->NgayXoa = $row["NgayXoa"];
				$gianHangDto->NguoiXoa = $row["NguoiXoa"];		
				$gianHangDto->NgayCapNhat = $row["NgayCapNhat"];	
				$gianHangDto->NguoiCapNhat = $row["NguoiCapNhat"];	
				$gianHangDto->Theme = $row["Theme"];	
				$gianHangDto->DaXoa = $row["DaXoa"];
				$gianHangDto->LuotXem = $row["LuotXem"];	
				$gianHangDto->ThongTin = $row["ThongTin"];	
				
				array_push($lstGianHang, $gianHangDto);
			}
			
			ConnectDB::CloseConnection();
			
		} catch (Exception $e) {
			$lstGianHang = array();
		}
		return $lstGianHang;
	}
	
	/**
	 * Lấy tất cả gian hàng xếp theo thứ tự Lượt xem
	 * edit by Thu Hà 20/6/2011
	 * $number: > 0: số dòng phải lấy ra, = 0: lấy tất cả
	 */
	public static function LayTatCaGianHangTheoLuotXem($number)
	{
		$lstGianHang = array();
		try 
		{
			if (!ConnectDB::OpenConnection())
				return FALSE;
						
			$strSQL = "SELECT * FROM `gian_hang` ORDER BY `LuotXem` DESC, `NgayTao` DESC";
			if ($number > 0)
				$strSQL .= " LIMIT 0, $number ";
			$strSQL .= " ;";
			
			//echo $strSQL;
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			if (!$result || mysql_num_rows($result) <= 0)
			{				
				return null;
			}
			
			while ($row = mysql_fetch_array($result))
			{
				$gianHangDto = new GianHangDTO();
				$gianHangDto->MaGianHang = $row["MaGianHang"];				
				$gianHangDto->MaNguoiDung = $row["MaNguoiDung"];	
				$gianHangDto->TenGianHang = $row["TenGianHang"];
				$gianHangDto->NgayTao = $row["NgayTao"];
				$gianHangDto->NgayXoa = $row["NgayXoa"];
				$gianHangDto->NguoiXoa = $row["NguoiXoa"];		
				$gianHangDto->NgayCapNhat = $row["NgayCapNhat"];	
				$gianHangDto->NguoiCapNhat = $row["NguoiCapNhat"];	
				$gianHangDto->Theme = $row["Theme"];	
				$gianHangDto->DaXoa = $row["DaXoa"];
				$gianHangDto->LuotXem = $row["LuotXem"];	
				$gianHangDto->ThongTin = $row["ThongTin"];
				
				if (file_exists($gianHangDto->ThongTin))
				{
					$lstThongTin = GianHangDAO::readXML($gianHangDto->ThongTin);
					$gianHangDto->Slogan = $lstThongTin["slogan"];
					$gianHangDto->DiaChi = $lstThongTin["diachi"];
					$gianHangDto->GioiThieu = $lstThongTin["gioithieu"];
					$gianHangDto->Footer = $lstThongTin["footer"];
					$gianHangDto->Logo = $lstThongTin["logo"];
					$gianHangDto->Banner = $lstThongTin["banner"];
					
				}
				
				array_push($lstGianHang, $gianHangDto);
			}
			
			ConnectDB::CloseConnection();
			
		} 
		catch (Exception $e) 
		{
			$lstGianHang = null;
		}
		return $lstGianHang;
	}
	//26/6/2011
	// Thu Hà 
	public static function LayThongTinGianHang_XML($maGianHang)
	{
		$gianHangDto = new GianHangDTO();
		try {
			if (!ConnectDB::OpenConnection())
			return FALSE;
			
			$strSQL = "SELECT *
					FROM `gian_hang`
					WHERE `MaGianHang` = $maGianHang AND `DaXoa` = 0;";
					
			//echo $strSQL;
			
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			if (!$result || mysql_num_rows($result) != 1)
			{
				return null;
			}
			
			$row = mysql_fetch_array($result);
			
			$gianHangDto->MaGianHang = $row["MaGianHang"];				
			$gianHangDto->MaNguoiDung = $row["MaNguoiDung"];	
			$gianHangDto->TenGianHang = $row["TenGianHang"];
			$gianHangDto->NgayTao = $row["NgayTao"];
			$gianHangDto->NgayXoa = $row["NgayXoa"];
			$gianHangDto->NguoiXoa = $row["NguoiXoa"];		
			$gianHangDto->NgayCapNhat = $row["NgayCapNhat"];	
			$gianHangDto->NguoiCapNhat = $row["NguoiCapNhat"];	
			$gianHangDto->Theme = $row["Theme"];	
			$gianHangDto->DaXoa = $row["DaXoa"];
			$gianHangDto->LuotXem = $row["LuotXem"];	
			$gianHangDto->ThongTin = $row["ThongTin"];	
			
			if (file_exists($gianHangDto->ThongTin))
			{
				$lstThongTin = GianHangDAO::readXML($gianHangDto->ThongTin);
				$gianHangDto->Slogan = $lstThongTin["slogan"];
				$gianHangDto->DiaChi = $lstThongTin["diachi"];
				$gianHangDto->GioiThieu = $lstThongTin["gioithieu"];
				$gianHangDto->Footer = $lstThongTin["footer"];
				$gianHangDto->Logo = $lstThongTin["logo"];
				$gianHangDto->Banner = $lstThongTin["banner"];
				
			}
			ConnectDB::CloseConnection();
			
		} catch (Exception $e) {
			$gianHangDto = null;
		}
		return $gianHangDto;
	}
	
	public static function LayDanhSachGianHangNguoiDungDKNhanMail($maND)
	{
		$lstGianHang = array();
		try 
		{
			if (!ConnectDB::OpenConnection())
				return FALSE;
						
			$strSQL = "SELECT * FROM `dang_ki_nhan_thong_tin` dk , gian_hang gh
						where dk.MaNguoiDung = gh.MaNguoiDung 
							and dk.MaNguoiDung = $maND;";
			
			//echo $strSQL;
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			if (!$result || mysql_num_rows($result) <= 0)
			{				
				return null;
			}
			
			while ($row = mysql_fetch_array($result))
			{
				$gianHangDto = new GianHangDTO();
				$gianHangDto->MaGianHang = $row["MaGianHang"];				
				$gianHangDto->MaNguoiDung = $row["MaNguoiDung"];	
				$gianHangDto->TenGianHang = $row["TenGianHang"];
				$gianHangDto->NgayTao = $row["NgayTao"];
				$gianHangDto->NgayXoa = $row["NgayXoa"];
				$gianHangDto->NguoiXoa = $row["NguoiXoa"];		
				$gianHangDto->NgayCapNhat = $row["NgayCapNhat"];	
				$gianHangDto->NguoiCapNhat = $row["NguoiCapNhat"];	
				$gianHangDto->Theme = $row["Theme"];	
				$gianHangDto->DaXoa = $row["DaXoa"];
				$gianHangDto->LuotXem = $row["LuotXem"];	
				$gianHangDto->ThongTin = $row["ThongTin"];
				
				array_push($lstGianHang, $gianHangDto);
			}
			
			ConnectDB::CloseConnection();
			
		} 
		catch (Exception $e) 
		{
			$lstGianHang = null;
		}
		return $lstGianHang;
	}
	
	//Đăng ký nhận email
	//$huyDK = 1, hủy, $huyDK = 0: ko hủy
	public static function DangKyNhanMail($maND, $maGianHang)
	{
		try 
		{
			if (!ConnectDB::OpenConnection())
				return FALSE;
						
			$strSQL = "INSERT INTO dang_ki_nhan_thong_tin (MaGianHang, MaNguoiDung, NgayDangKy)
						VALUES ($maGianHang, $maND, now())";
			
			//echo $strSQL;
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			
			ConnectDB::CloseConnection();
			
		} 
		catch (Exception $e) 
		{
			return false;
		}
		return $result;
	}
	//Hủy đăng ký nhận email
	//$huyDK = 1, hủy, $huyDK = 0: ko hủy
	public static function HuyNhanEmail($maND, $maGianHang)
	{
		try 
		{
			if (!ConnectDB::OpenConnection())
				return FALSE;
						
			$strSQL = "DELETE FROM dang_ki_nhan_thong_tin 
						WHERE MaGianHang = $maGianHang AND 
							 MaNguoiDung = $maND ";
			
			//echo $strSQL;
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			
			ConnectDB::CloseConnection();
			
		} 
		catch (Exception $e) 
		{
			return false;
		}
		return $result;
	}
	//Kiểm tra xem có nhận email chưa
	//$huyDK = 1, hủy, $huyDK = 0: ko hủy
	public static function KiemTraDangKyNhanEmail($maND, $maGianHang)
	{
		try 
		{
			if (!ConnectDB::OpenConnection())
				return FALSE;
						
			$strSQL = "SELECT * FROM `dang_ki_nhan_thong_tin` dk 
						where dk.MaGianHang = $maGianHang 
						AND dk.MaNguoiDung = $maND;";
			
			//echo $strSQL;
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			if ($result && mysql_num_rows($result) > 0)
			{
				return true;
			}			
			
			ConnectDB::CloseConnection();
			
		} 
		catch (Exception $e) 
		{
			return false;
		}
	}
}
?>